package com.example.springboot_sp.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_sp.common.ResultBlog;
import com.example.springboot_sp.entity.User;
import com.example.springboot_sp.exception.CustomException;
import com.example.springboot_sp.service.impl.CategoryServiceImpl;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.example.springboot_sp.common.Result;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot_sp.service.ICategoryService;
import com.example.springboot_sp.entity.Category;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郭守军
 * @since 2022-09-04
 */
@RestController
@RequestMapping("/category")
        public class CategoryController {
    
        @Resource
        private CategoryServiceImpl categoryService;

        @Resource
        private HttpServletRequest request;

        public User getUser() {
                return (User) request.getSession().getAttribute("user");
        }
        @PostMapping
        public ResultBlog<?> save(@RequestBody Category category) {
                LambdaQueryWrapper<Category> query = Wrappers.<Category>lambdaQuery().eq(Category::getName, category.getName());
                List<Category> list = categoryService.list(query);
                if (CollUtil.isNotEmpty(list)) {
                        throw new CustomException("-1", "名称重复");
                }
                return ResultBlog.success(categoryService.save(category));
        }

        @PutMapping
        public ResultBlog<?> update(@RequestBody Category category) {
                return ResultBlog.success(categoryService.updateById(category));
        }

        @DeleteMapping("/{id}")
        public ResultBlog<?> delete(@PathVariable Long id) {
                categoryService.removeById(id);
                return ResultBlog.success();
        }

        @GetMapping("/{id}")
        public ResultBlog<?> findById(@PathVariable Long id) {
                return ResultBlog.success(categoryService.getById(id));
        }

        @GetMapping
        public ResultBlog<?> findAll() {
                return ResultBlog.success(categoryService.list());
        }

        @GetMapping("/page")
        public ResultBlog<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
                LambdaQueryWrapper<Category> query = Wrappers.<Category>lambdaQuery().like(Category::getName, name);
                return ResultBlog.success(categoryService.page(new Page<>(pageNum, pageSize), query));
        }

}

